<template>
  <view>
    <button @click="upload" class="but">户主身份证识别</button>
    <div class="card_info">
        <view class="li">姓名：{{caidData.LD_NAME || ''}}</view>
        <view class="li">性别：{{caidData.SEX || ''}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;民族：{{caidData.LD_MZ || ''}}</view>
        <view class="li">出生：{{caidData.LD_BIRTHDAY || ''}}</view>
        <view class="li">住址：{{caidData.LD_ADDRESS || ''}}</view>
        <view class="li">身份证号码：{{caidData.LD_ID || ''}}</view>
        <view class="user_pic">
            <image :src="$host+caidData.LD_IMG" v-if="caidData.LD_IMG"></image>
            <image src="/static/img/default.png" v-else></image>
            <!-- <image :src="userimg || '/static/img/default.png'" @click="uploadimg"></image> -->
            <text>点击拍照</text>
        </view>
    </div>
    <view class="uni-list">
        <view class="uni-list-cell">
            <view class="uni-list-cell-left">
                <img src="/static/img/drop-down.png" class="down_img"><text>保障类型</text>
            </view>
            <view class="uni-list-cell-db">
                <picker @change="bindsecurityChange" :value="securityindex" :range="securityType" disabled>
                    <text class="uni-input">{{caidData.CLASS1}}</text>
                    <img src="/static/img/right.png" class="right_img">
                </picker>
            </view>
        </view>
        <view class="uni-list-cell">
            <view class="uni-list-cell-left">
                <img src="/static/img/drop-down.png" class="down_img"><text>救助类别</text>
            </view>
            <view class="uni-list-cell-db">
                <picker @change="bindhouseholdChange" :value="householdindex" :range="householdType" disabled>
                    <text class="uni-input">{{caidData.CLASS2}}</text>
                    <img src="/static/img/right.png" class="right_img">
                </picker>
            </view>
        </view>
    </view>
    <button @click="goEditallowances" class="but">确定</button>
  </view>
</template>

<script>
import { guid } from '@/utils/tool.js'
  export default {
    data() {
      return {
            $host: this.$host,
            isWindows:false,
            dataList: [],
            securityType: [],
            householdType: [],
            securityindex: 0,
            householdindex: 0,
            caidData: {},
            userimg: ''
      }
    },
    onLoad() {
        let that = this;
        uni.getSystemInfo({
            success: function (res) {
                if (res.platform == 'windows') {
                    that.isWindows = true
                }
            }
        });
        uni.getStorage({
            key: 'navdata',
            success: function (res) {
                that.caidData.CLASS1 = res.data.CLASS1
                that.caidData.CLASS2 = res.data.CLASS2
            }
        });
        this.globalGetData()
        this.caidData.LM_GUID = guid()
    },
    methods: {
        // 保障类型
        bindsecurityChange(e) {
            this.securityindex = e.target.value
            this.caidData.CLASS1 = this.securityType[this.securityindex]
            this.householdType = []
            this.dataList.forEach(item => {
                if (item.dict_sub2 == this.caidData.CLASS1) {
                    this.householdType.push(item.dict_sub3)
                }
            });
            this.caidData.CLASS2 = this.householdType[this.householdindex]
        },
        // 救助类别
        bindhouseholdChange(e) {
            this.householdindex = e.target.value
            this.caidData.CLASS2 = this.householdType[this.householdindex]
        },
        goEditallowances () {
            if (this.caidData.LD_ID && this.caidData.CLASS2 && this.caidData.CLASS1 && this.caidData.LD_IMG) {
                console.log(encodeURIComponent(JSON.stringify(this.caidData)))
                this.$http.get('checkadd', {params:{ID: this.caidData.LD_ID, NAME: this.caidData.LD_NAME}}).then(res => {
                    if(res.data.code == 0){
                        uni.navigateTo({
                            url: './allowancesFill?data=' + encodeURIComponent(JSON.stringify(this.caidData))
                        })
                    } else {
                        uni.showToast({
                            title: res.data.message,
                            duration: 2000,
                            icon: 'none'
                        });
                    }
                })
            } else {
                uni.showToast({
                    title: '请完善信息',
                    duration: 2000,
                    icon: 'none'
                });
            }
        },
        // 识别身份证
        upload() {
            if (this.isWindows) {
                uni.showModal({
                    title: '提示',
                    showCancel: false,
                    content: '不支持电脑上传图片，请用手机操作'
                });
                return false
            }
            let that = this;
            console.log(that.caidData.LM_GUID)
            uni.chooseImage({
                count: 1,
                sizeType: ['compressed'],
                sourceType: ['camera'],
                success: function (self) {
                    that.$http.upload('Getidcard', {
                        filePath: self.tempFilePaths[0], 
                        name: encodeURI('filePath'),
                        formData:{
                            "LM_GUID":that.caidData.LM_GUID
                        }
                    }).then(res => {
                        console.log(JSON.parse(res))
                        if (JSON.parse(res).error_code === 0) {
                            let data = JSON.parse(res).data.IDCARD
                            that.caidData.LD_ID = data.ID
                            that.caidData.LD_NAME = data.NAME
                            that.caidData.SEX = data.SEX
                            that.caidData.LD_BIRTHDAY = data.BIRTHDAY
                            that.caidData.LD_ADDRESS = data.ADDRESS
                            that.caidData.LD_MZ = data.MZ
                            that.caidData.LD_IMG = data.photo
                            that.$set(that.caidData)
                        } else {
                            uni.showModal({
                                title: '提示',
                                showCancel: false,
                                content: JSON.parse(res).error_msg
                            });
                            // uni.showToast({
                            //     title: '识别失败',
                            //     duration: 2000,
                            //     icon: 'none'
                            // });
                        }
                    })
                }
            })
        },
        // 录入照片
        uploadimg() {
            if (this.isWindows) {
                uni.showModal({
                    title: '提示',
                    showCancel: false,
                    content: '不支持电脑上传图片，请用手机操作'
                });
                return false
            }
            let that = this
            uni.chooseImage({
                count: 1,
                sizeType: ['compressed'],
                sourceType: ['camera'],
                success: function (self) {
                    that.$http.upload('uploadLDIMG', {filePath: self.tempFilePaths[0], name: encodeURI('filePath'),formData:{
                        "LM_GUID":that.caidData.LM_GUID,
                        "LD_ID":that.caidData.ID,
                        "ckface":"true"
                    }
                    }).then(res => {
                        if (JSON.parse(res).error_code == 0) {
                            let QC = JSON.parse(res).data.QC; 
                            if (QC.score == 1) {
                                that.caidData.LD_IMG = JSON.parse(res).data.RPATH
                                that.userimg = JSON.parse(res).data.APATH
                            } else {
                                uni.showModal({
                                    title: '提示',
                                    showCancel: false,
                                    content: QC.message
                                });
                            }
                        } else {
                            uni.showModal({
                                title: '提示',
                                showCancel: false,
                                content: JSON.parse(res).error_msg
                            });
                        }
                    })
                }
            })
        },
        // 获取户籍和保障类型
        globalGetData() {
            this.$http.get('dicts', {params: {dict_sub: '系统参数', dict_sub1: '保障类型',dict_sub2: '',showField:'dict_sub2,dict_sub3'}}).then(res => {
                this.dataList = res.data
                let securityTypeArr = []
                res.data.forEach(item => {
                    securityTypeArr.push(item.dict_sub2)
                });
                this.securityType = Array.from(new Set(securityTypeArr))
            })
        }
    }
  }
</script>

<style lang="scss" scoped>
.but{
    background-color: #1EC88B;
    border: none;
    height: 94upx;
    line-height: 94upx;
    text-align: center;
    border-radius: 10upx;
    color: #fff;
    margin:20upx;
    font-size: 36upx;
}
.card_info{
    border-top: 1px solid #bbb;
    border-bottom: 1px solid #bbb;
    font-size: 28upx;
    color: #101010;
    padding: 28upx;
    position: relative;
    .li{
        line-height: 60upx;
    }
    .user_pic{
        position: absolute;
        right: 28upx;
        font-size: 24rpx;
        text-align: center;
        top: 28upx;
        image{
            display: block;
            margin-bottom: 10rpx;
            width: 150upx;
            height: 160upx;
        }
    }
}
.uni-list-cell{
    border-top: solid 1px #DCDFE6;
    border-bottom: solid 1px #DCDFE6;
    margin: 12upx 0;
    display: flex;
    height: 100upx;
    align-items: center;
    font-size: 32upx;
    color: #6D6E71;
    padding: 0 30upx;
    justify-content: space-between;
    .uni-list-cell-left{
        .down_img{
            margin-right: 20upx;
            width: 44upx;
            height: 44upx;
            vertical-align: middle;
        }
        text{
            vertical-align: middle;
        }
    }
    .uni-list-cell-db{
        width: 50%;
        text-align: right;
    }
    .uni-input{
       vertical-align: middle; 
    }
    .right_img{
        width: 50upx;
        height: 50upx;
        vertical-align: middle;
    }
}
</style>
